//#include "sensor_service.h"

#include "app.h"

#if NO_PRINTF
#define printf(...)
#endif

static void mesh_adv_time(void* arg);
static void mesh_scan_time(void* arg);

#ifdef CANNON_V2
char board_name[20] = "CANNON_V2";
#endif
#ifdef CANNON_V1
char board_name[20] = "CANNON_V1";
#endif

void on_ready(void)
{
    uint8_t tx_power_level = 7;
    uint16_t scan_interval = 100, adv_interval = 1000;

    uint8_t bdAddr[6] = {0x03,0x03,0x03,0x03,0x03,0x05};
    char name[32];

    BSP_LED_On(LED0);
    HCI_get_bdAddr(bdAddr);
    sprintf(name, "%s %01x%01x", board_name, bdAddr[0], bdAddr[1]);
    /*Config Adv Parameter And Ready to Adv*/
    /*device*/
    ble_set_adv_param(name, bdAddr, tx_power_level, adv_interval);
  
    /*Host*/
    //ble_host_set_scan_param(bdAddr, tx_power_level, scan_interval);
    
    //ble_host_start_scan();
    ble_device_start_advertising();
    //run_after_delay(mesh_scan_time, NULL, 2000);
}
/*adv time*/
static void mesh_adv_time(void* arg)
{
    ble_host_stop_scan();
    ble_device_start_advertising();
    run_after_delay(mesh_scan_time, NULL, 2000);
}
/*scan time*/
static void mesh_scan_time(void* arg)
{
    ble_device_stop_advertising();
    ble_host_start_scan();
    run_after_delay(mesh_adv_time, NULL, 2000);
}

void ble_host_on_device_info(scan_device_found_info device_info)
{
//    if(device_info.RSSI > -60) {
//        BSP_LED_Toggle(LED0);
//        ble_host_connect(device_info.bdaddr);
//    }
//    } else {
//        //ble_host_start_scan();
//    }
//      printf("devie_info: %x,%x,%x,%x,%x,%x\n",device_info.bdaddr[0],device_info.bdaddr[1],
//                                              device_info.bdaddr[2],device_info.bdaddr[3],device_info.bdaddr[4],device_info.bdaddr[5]);
//      printf("d\n");


}

void ble_host_on_message(uint8_t type, uint16_t length, uint8_t* value)
{
    BSP_LED_Toggle(LED0);
    /*echo*/
    *value = 1;
    ble_host_send(type, length, value);
    printf("receive data:%x,%x,%x\n\r",type,length,value[0]);
}

void ble_host_on_connect()
{

}
